﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.member;
using Commerce.Common;

namespace CommerceForUmbraco.usercontrols.usercontrols.commerce
{
    public partial class PWLogin : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string url = System.Web.HttpContext.Current.Request.QueryString["ReturnUrl"];
            //Label urlText = (Label)LoginView1.FindControl("urlText");            
            if (!string.IsNullOrEmpty(url))
            {
                Session["ReturnUrl"] = Server.UrlDecode(url.ToString());
            }

            if (Request.IsAuthenticated)
            {
                Member m = Member.GetCurrentMember();
                //Literal Literal1 = (Literal)LoginView1.FindControl("Literal1");

                string ReturnUrl = (string)(Session["ReturnUrl"]);
                if (!string.IsNullOrEmpty(ReturnUrl))
                {
                    Session.Remove("ReturnUrl");
                    Response.Redirect(ReturnUrl);
                }
                else
                {
                    Response.Redirect("~/account/", true);
                }
            }
            else
            {
                Session["PreviousUserName"] = Utility.GetUserName();
            }

        }

        protected void Login1_LoggedIn(object sender, EventArgs e)
        {
            string prevUserName = (string)(Session["PreviousUserName"]);

            string sUserName = ((Login)sender).UserName;

            if (string.IsNullOrEmpty(sUserName) == false)
            {
                OrderController.MigrateCart(prevUserName, sUserName);
            }
        }
    }
}